/* linkit.js
 * By François Leporcq
 * MIT Licensed.
 */
var Dragger = $Class.extend({
    _className:"Dragger",
    init: function(workspace){
        this._workspace = workspace;
        this._shape = null;
        this._offset = null;
    },
    select: function(event, shape){
        this._shape = shape;
        var cursor = this._getCursor(event);
        var origin = this._shape.getOrigin();
        this._offset = new Point(cursor.getX() - origin.getX(), cursor.getY() - origin.getY());
    },
    move:function(event){
        if(this._shape != null){
            var cursor = this._getCursor(event);
            var newOrigin = new Point(cursor.getX() - this._offset.getX(), cursor.getY() - this._offset.getY());
            this._shape.setOrigin(newOrigin);
            var links = this._shape.getLinks();
            if(links != null){
                for(var i = 0 ; i < links.length ; i++){
                    links[i].computeStartEnd();
                    this._workspace.draw(links[i]);
                }
            }
            this._workspace.draw(this._shape);
        }
    },
    deselect: function(){
        this._shape = null;
        this._offset = null;
    },
    _getCursor: function(event) {
        var event = event || window.event;
        var cursor = new Point();
        if (event.pageX || event.pageY) {
            var x = event.pageX,
                y = event.pageY;
        }
        else {
            var x = event.clientX +
                (document.documentElement.scrollLeft ||
                    document.body.scrollLeft) -
                document.documentElement.clientLeft,
                y = event.clientY +
                (document.documentElement.scrollTop ||
                    document.body.scrollTop) -
                document.documentElement.clientTop;
        }
        cursor.setX(x);
        cursor.setY(y);
        return cursor;
    }
});
